package visiopuce.ui.listener;

import java.awt.Desktop;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

import visiopuce.ApplicationContexte;

public class ExportXLSListener extends SelectionAdapter {

	private TableViewer tableGenes;
	private String titre;

	public ExportXLSListener(TableViewer tableGenes, String titre) {
		super();
		this.tableGenes = tableGenes;
		this.titre = titre;
	}

	public void widgetSelected(SelectionEvent e) {
		try {

			HSSFWorkbook wb = new HSSFWorkbook();

			// add a worksheet
			HSSFSheet sheet = wb.createSheet("Gènes");

			// // shade the background of the header row
			// XSSFCellStyle headerStyle = wb.createCellStyle();
			// headerStyle.setFillForegroundColor(IndexedColors.LEMON_CHIFFON.getIndex());
			// headerStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
			// headerStyle.setBorderTop(CellStyle.BORDER_THIN);
			// headerStyle.setBorderBottom(CellStyle.BORDER_THIN);
			// headerStyle.setBorderLeft(CellStyle.BORDER_THIN);
			// headerStyle.setBorderRight(CellStyle.BORDER_THIN);
			// headerStyle.setAlignment(HorizontalAlignment.CENTER);

			Table table = tableGenes.getTable();
			TableColumn[] columns = table.getColumns();
			int rowIndex = 0;
			int cellIndex = 0;
			Row header = sheet.createRow((short) rowIndex++);
			for (TableColumn column : columns) {
				String columnName = column.getText();
				Cell cell = header.createCell(cellIndex++);
				cell.setCellValue(column.getText());
				// cell.setCellStyle(headerStyle);
			}

			// add data rows
			TableItem[] items = table.getItems();
			for (TableItem item : items) {
				// create a new row
				Row row = sheet.createRow((short) rowIndex++);
				cellIndex = 0;

				for (int i = 0; i < columns.length; i++) {
					// create a new cell
					// String columnName = tableColumnNames[i];
					Cell cell = row.createCell(cellIndex++);

					// // set the horizontal alignment (default to RIGHT)
					// XSSFCellStyle cellStyle = wb.createCellStyle();
					// ha = HorizontalAlignment.RIGHT;
					// cellStyle.setAlignment(ha);
					// cell.setCellStyle(cellStyle);

					// set the cell's value
					String text = item.getText(i);
					cell.setCellValue(text);
				}
			}

			// autofit the columns
			for (int i = 0; i < columns.length; i++) {
				sheet.autoSizeColumn((short) i);
			}
			File fichierResultat = new File(ApplicationContexte.REPERTOIRE_PROTOCOLE + titre + "_genes.xls");
			FileOutputStream fos = new FileOutputStream(fichierResultat);
			wb.write(fos);
			fos.close();
			Desktop desktop = Desktop.getDesktop();
			desktop.open(fichierResultat);

		} catch (FileNotFoundException ex) {
			ex.printStackTrace();
		} catch (IOException ev) {
			ev.printStackTrace();
		}

	}

}
